package com.txhy.certificatechain.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.txhy.certificatechain.R;
import com.txhy.certificatechain.api.UrlAddress;
import com.txhy.certificatechain.model.bean.MailListBean;
import com.txhy.certificatechain.ui.manager.ImageLoaderManager;

import java.util.List;

/*
 * -----------------------------------------------------------------
 * Copyright (C) 2018-2030, by your company, All rights reserved.
 * -----------------------------------------------------------------
 * 文    件: ContactsAdapter.java
 * 作    者: 侯建军
 * 版    权: 1.0.0
 * 描    述: 联系人适配器
 * 创建时间: 2018/9/20 10:27
 * 更新时间：2018/9/20 10:27
 * -----------------------------------------------------------------
 * 2018/9/20 10:27: 创建 ContactsAdapter.java (侯建军);
 * -----------------------------------------------------------------
 */
public class ContactsAdapter extends BaseAdapter {
    private List<MailListBean.DataEntity> list;
    private LayoutInflater inflater;
    public ViewHolder holder;
    private Context context;

    public ContactsAdapter(Context context, List<MailListBean.DataEntity> list) {
        inflater = LayoutInflater.from(context);
        this.list = list;
        this.context=context;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.list_item, null);
            holder.tv_word = convertView.findViewById(R.id.tv_word);
            holder.tv_name = convertView.findViewById(R.id.tv_name);
            holder.img_photo = convertView.findViewById(R.id.img_photo);


            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        String word = list.get(position).firstLetter;
        holder.tv_word.setText(word);
        holder.tv_name.setText(list.get(position).getCardName());
        ImageLoaderManager.LoadImage(context, UrlAddress.IMGBASE_URL+list.get(position).getAvatar(),holder.img_photo);
//        holder.img_photo.setImageResource(list.get(position).imgPhoto);

        //将相同字母开头的合并在一起
        if (position == 0) {
            //第一个是一定显示的
            holder.tv_word.setVisibility(View.VISIBLE);
        } else {
            //后一个与前一个对比,判断首字母是否相同，相同则隐藏
            String headerWord = list.get(position - 1).firstLetter;
            if (word.equals(headerWord)) {
                holder.tv_word.setVisibility(View.GONE);
            } else {
                holder.tv_word.setVisibility(View.VISIBLE);
            }
        }
        return convertView;
    }
    public class ViewHolder {
        private TextView tv_word;
        private TextView tv_name;
        private ImageView img_photo;
    }
    public void notifyCheck(List<MailListBean.DataEntity> list) {
        this.list = list;
        notifyDataSetChanged();
    }
}